package db;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.TableColumn;

public class themlinhvuc {

	protected Shell shell;
	private Table tablelinhvuc;
	private Text txtmalinhvuc;
	private Text txttenlinhvuc;
	MessageBox ms;
	private int thuchien=0;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			themlinhvuc window = new themlinhvuc();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				if(thuchien==0)
				{
					try {
						loadata();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					thuchien=1;
				}
				//display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(521, 367);
		shell.setText("Thêm lĩnh vực");
		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBounds(28, 67, 447, 74);
		
		
		Button bntsua = new Button(composite, SWT.NONE);
		bntsua.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(txtmalinhvuc.getText().equals(""))
				{
				  ms=new MessageBox(shell);
				  ms.setMessage("bạn chưa chọn thông tin cần sửa");
				  ms.open();
				  
					
				}
				else
				{
					ms=new MessageBox(shell,SWT.ICON_QUESTION|SWT.YES|SWT.NO);
					ms.setMessage("bạn thật sự muốn sửa không");
					int i=ms.open();
					if(i==64)
					{
							linhvuc lv=new linhvuc(txtmalinhvuc.getText(), txttenlinhvuc.getText());
							lv.Update();
							try {
								loadata();
							} catch (SQLException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
					}
				}
			}
		});
		bntsua.setBounds(344, 10, 59, 35);
		
		
		Button bntxoa = new Button(composite, SWT.NONE);
		bntxoa.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(txtmalinhvuc.getText().equals(""))
				{
					  ms=new MessageBox(shell);
					  ms.setMessage("bạn chưa chọn thông tin cần sửa");
					  ms.open();
				
				}
				else
				{
					ms=new MessageBox(shell,SWT.ICON_QUESTION|SWT.YES|SWT.NO);
					ms.setMessage("bạn thực sự muốn xóa không");
					int i=ms.open();
					if(i==64)
					{
						
						linhvuc lv=new linhvuc(txtmalinhvuc.getText(), txttenlinhvuc.getText());
						lv.Delete(txtmalinhvuc.getText());
						try {
							loadata();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					
				}
			}
		});
		bntxoa.setBounds(244, 10, 59, 35);
		
		Button bntxem = new Button(composite, SWT.NONE);
		bntxem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					loadata();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		bntxem.setBounds(138, 10, 59, 35);
		
		
		Button bntthem = new Button(composite, SWT.NONE);
		bntthem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(!txtmalinhvuc.getText().equals("")&&! txttenlinhvuc.getText().equals(""))
				{
					ms=new MessageBox(shell,SWT.ICON_QUESTION|SWT.YES|SWT.NO);
					ms.setMessage("bạn thực muốn thêm không!");
					int i=ms.open();
					if(i==64)
					{
						
						linhvuc lv=new linhvuc(txtmalinhvuc.getText(), txttenlinhvuc.getText());
						lv.Save();
						try {
							loadata();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					
				}
				else
				{
					ms=new  MessageBox(shell);
					ms.setMessage("bạn chưa điền thông tin cần thêm vào");
					ms.open();
					
				}
			}
		});
		bntthem.setBounds(33, 10, 59, 35);
		
		Label lblNewLabel = new Label(composite, SWT.CENTER);
		lblNewLabel.setBounds(33, 49, 55, 15);
		lblNewLabel.setText("THÊM");
		
		Label label = new Label(composite, SWT.CENTER);
		label.setText("XEM");
		label.setBounds(138, 49, 55, 15);
		
		Label label_1 = new Label(composite, SWT.CENTER);
		label_1.setText("XÓA");
		label_1.setBounds(244, 51, 55, 15);
		
		Label label_2 = new Label(composite, SWT.CENTER);
		label_2.setText("SỮA");
		label_2.setBounds(344, 51, 55, 15);
		
		tablelinhvuc = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		tablelinhvuc.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int i=tablelinhvuc.getSelectionIndex();
				txtmalinhvuc.setText(tablelinhvuc.getItem(i).getText(0));
				txttenlinhvuc.setText(tablelinhvuc.getItem(i).getText(1));
			}
		});
		tablelinhvuc.setBounds(28, 147, 447, 172);
		tablelinhvuc.setHeaderVisible(true);
		tablelinhvuc.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(tablelinhvuc, SWT.NONE);
		tblclmnNewColumn.setWidth(72);
		tblclmnNewColumn.setText("STT");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(tablelinhvuc, SWT.NONE);
		tblclmnNewColumn_1.setWidth(171);
		tblclmnNewColumn_1.setText("MÃ LĨNH VỰC");
		
		TableColumn tblclmnNewColumn_2 = new TableColumn(tablelinhvuc, SWT.NONE);
		tblclmnNewColumn_2.setWidth(212);
		tblclmnNewColumn_2.setText("TÊN LĨNH VỰC");
		
		Group group = new Group(shell, SWT.NONE);
		group.setBounds(28, 0, 447, 61);
		
		txtmalinhvuc = new Text(group, SWT.BORDER);
		txtmalinhvuc.setBounds(153, 10, 154, 21);
		
		txttenlinhvuc = new Text(group, SWT.BORDER);
		txttenlinhvuc.setBounds(153, 37, 154, 21);
		
		Label lblNewLabel_1 = new Label(group, SWT.NONE);
		lblNewLabel_1.setBounds(48, 13, 94, 15);
		lblNewLabel_1.setText("MÃ LĨNH VỰC");
		
		Label lblNewLabel_2 = new Label(group, SWT.NONE);
		lblNewLabel_2.setBounds(48, 40, 94, 15);
		lblNewLabel_2.setText("TÊN LĨNH VỰC");
		

	}
	public void loadata() throws SQLException
	{
		tablelinhvuc.removeAll();
		linhvuc lv=new linhvuc();
		ResultSet rs= lv.SelectAll();
		while(rs.next())
		{
			TableItem item=new TableItem(tablelinhvuc, 0);
			item.setText(new String[] {rs.getString(1),rs.getString(2),rs.getString(3)});
			
		}
		
		
	}
}
